package com.accp.electronicdocumentmanagementsystem.action;

import com.accp.electronicdocumentmanagementsystem.biz.Entrbiz;
import com.accp.electronicdocumentmanagementsystem.pojo.Entr;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/entr")
public class Entraction {

    @Autowired(required = false)
    private Entrbiz entrbiz;

    @RequestMapping("/addEntr")
    public Map<String,String> addEntr(@RequestBody Entr entr){
        Map<String,String> map = new HashMap<>();
        if (entrbiz.insertEntr(entr)){
            map.put("code","200");
            return map;
        }else {
            map.put("code","401");
            return map;
        }
    }
    @RequestMapping("/delEntr/{id}")
    public Map<String,String> delEntr(@PathVariable Long id){
        Map<String,String> map = new HashMap<>();
        if (entrbiz.deleteEntr(id)){
            map.put("code","200");
            return map;
        }else {
            map.put("code","401");
            return map;
        }
    }
    @RequestMapping("/updEntr")
    public Map<String,String> updEntr(@RequestBody Entr entr) {
        Map<String,String> map = new HashMap<>();
        if (entrbiz.updateEntr(entr)){
            map.put("code","200");
            return map;
        }else {
            map.put("code", "401");
            return map;
        }
    }
    @RequestMapping("/selEntr/{name}/{page}/{size}")
    public PageInfo selEntr(@PathVariable String name, @PathVariable String page, @PathVariable String size) {
        System.out.println(name+" "+page+" "+size);
        if (name.equals("undefined")) {
            name = "全部";
        }
        if (page.equals("undefined")) {
            page = "1";
        }
        if (size.equals("undefined")) {
            size = "5";
        }
        return entrbiz.selEntr(name,Integer.parseInt(page),Integer.parseInt(size));
    }

    @RequestMapping("/selEntr/{id}")
    public Entr selEntr(@PathVariable Long id){
        return entrbiz.selEntr(id);
    }
}
